<?php
/**
 * model基类
 * 
 * @link http://www.milkphp.com/
 * @author donwa <jluzh@qq.com>
 * @license New BSD License {@link http://www.milkhphp.com/license/}
 * 
 */
class lib_db_model{
	/**
	 * 查询数组
	 * @var array
	 * 
	 * $_query['name']当前model的名字
	 * $_query['set'] 设置的值
	 * $_query['find'] 查找对象设置的条件
	 * $_query[]
	 */
	protected $_query;
	
	function __construct(){
		if(!empty($this->_table)){
			$this->_query['table']=$this->_table;
		}else{
			$this->_query['table']=substr(get_class($this),6);
		}
	}
	
/**
 * ---查询结果返回-----
 */
	/**
	 * 获取查询结果
	 * 返回array
	 */
	function get($num=-1){
		if($num!=-1)
			$this->limit($num,1);
		$list = milkphp()->cache('db_'.$this->_query['table'])->get($this->_query);
		if(!is_null($list))
			return $list;
		$list = milkphp()->db()->connect()->get($this->_query);
		if($this->_cache){
			milkphp()->cache('db_'.$this->_query['table'])->set($this->_query,$list,$this->_cache);
		}
		if($num!=-1)return $list[0];
		return $list;
	}
	
	/**
	 * 返回查询结果数量
	 * @param $this_db
	 * @return int
	 */
	function count(){
		return milkphp()->db()->connect()->count($this->_query);
	}
	/**
	 * ---------------添加-----------------
	 * 返回添加后的id
	 */
	function add(){
		return milkphp()->db()->connect()->add($this->_query);
	}
	
	/**
	 * ---------------编辑-----------------
	 */
	function save(){
		return milkphp()->db()->connect()->save($this->_query);
	}
	/**
	 * ---------------删除-----------------
	 * @return bool
	 */
	function del(){
		return milkphp()->db()->connect()->del($this->_query);
	}

/**
 * ---------查询条件设置------------
 * begin
 */
	/**
	 * 设置键值
	 * @param string $name
	 * @param string $value
	 */
	function set($field,$value=NULL){
		if(is_array($field)){
			foreach($field as $k=>$v){
				$this->set($k,$v);
			}
			return $this;
		}
		/*
		if(!is_array($this->_model->_field[$field])&& get_class($this->_model) != 'stdClass'){
			return $this;
		}
		*/
		if(!is_array($this->_field[$field]))
			return $this;
			
		if(get_magic_quotes_gpc()){
			$value = stripcslashes($value);
		}
		if(method_exists(get_class($this),"set".$field)){
			$this->_query['set'][$field] = $this->{"set".$field}($value);
			return $this;
		}
//		$this->_query['set'][$field]=mysql_real_escape_string($value);
		$this->_query['set'][$field]=$value;
		return $this;
	}
	
	/**
	 * 选择要查询的键值
	 * return model object
	 */
	function select(){
		$fields = func_get_args();
		foreach($fields as $field){
			$fieldArr = explode(" ",$field);
			$fieldName=$fieldArr[0];
			$niceName = empty($fieldArr[1])?$fieldName:$fieldArr[1];
			if(is_array($this->_model->_field[$fieldName])){
				$fieldName = $this->_model->_field[$field]['field'];
				$this->_query['select'][$niceName] = $fieldName;
			}elseif(get_class($this->_model) == 'stdClass'){
				$this->_query['select'][$niceName] = $fieldName;
			}
		}
		return $this;
	}
	
	/**
	 * 设置条件
	 * @param $str
	 * @param $value
	 * @return model object
	 * 
	 * @code
	 * 
	 * #传入字符串
	 * m('user')->find('username','donwa')->find('password','123456');
	 * 
	 * #传入数组
	 * $find['username'] = 'donwa';
	 * $find['password'] = '123456';
	 * m('user')->find($find);
	 * 
	 * @endcode
	 * 
	 */
	function find($str,$value="",$isKey=0){
 		$value = addslashes(stripslashes($value));
		/**
		 * 支持数组操作
		 */
 		if(is_array($str)&&empty($value)){
			foreach ($str as $key => $val){
				$this->find($key,$val);
			}
			return $this;
		}
		
		/**
		 * 普通赋值操作
		 * #$str = 'OR password !='
		 * #$value = '123456'
		 */
		else if(is_string($str)&&$value!=""){
			$temp = explode(" ",$str,3);
			/**
			 * 连接符 AND 或者 OR
			 * #$connect = 'OR'
			 */
			if(strtoupper($temp[0])=="OR"||strtoupper($temp[0])=="AND"){
				$connect=strtoupper($temp[0]);
				$i=1;
			}else{
				$connect = 'AND';
				$i=0;
			}
			
			/**
			 * 键值
			 * @var string
			 * #$field = 'password';
			 */
			$field= $temp[$i];
			
			/**
			 * 操作符
			 * @var string
			 * #$condition = '!=';
			 */
			$condition = empty($temp[$i+1])?"=":$temp[$i+1];
			
			/**
			 * 判断操作符的合法性
			 */
			if(!preg_match("/^[=|>|<|!|like|LIKE]+$/",$condition))
				return $this;
			#array('OR','password','!=','123456')
			$this->_query['find'][]=array($connect,$field,$condition,$value,$isKey);
		}
		return $this;
	}
	
	function orFind($str,$value="",$isKey=0){
		/**
		 * 支持数组操作
		 */
 		if(is_array($str)&&empty($value)){
			foreach ($str as $key => $val){
				$this->orfind($key,$val);
			}
			return;
		}
		$this->find('OR '.$str,$value);
		return $this;
	}
	
	/**
	 * 查询条数
	 * @param $start
	 * @param $num
	 * @return lib_db object
	 * 
	 * @code
	 * m('article')->limit(0,15);
	 * @endcode
	 */
	function limit($start=0,$num=0){
		$this->_query['limit']=array(max(0,$start),max(1,$num));
		return $this;
	}
	
	/**
	 * 按页查询
	 * @param $num
	 * @param $size
	 * @return lib_db object
	 * 
	 * @code
	 * #每页15条，返回第一页
	 * m('article')->page(1,15);
	 * @endcode
	 */
	function page($num=1,$size=15){
		$start=((int)$num-1)*(int)$size;
		return $this->limit($start,(int)$size);
	}
	
	/**
	 * 设置查询顺序
	 * @param $field
	 * @param $order {0,1}
	 * @return lib_db object
	 */
	function order($field,$order=0){
		$this->_query['order']=array($field,$order);
		return $this;
	}
/**
 * ---------查询条件设置------------
 * end
 */
}
